package com.ymtms.statisticalform.service.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ymtms.statisticalform.dao.EmpBusinessMapper;
import com.ymtms.statisticalform.dao.EmpMapper;
import com.ymtms.statisticalform.pojo.Emp;
import com.ymtms.statisticalform.pojo.EmpBusiness;
import com.ymtms.statisticalform.service.EmpBusinessService;
import com.ymtms.statisticalform.vo.GridRequest;
import com.ymtms.statisticalform.vo.GridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class EmpBusinessServiceImpl implements EmpBusinessService {
    @Autowired
    EmpBusinessMapper empBusinessMapper;
    @Autowired
    EmpMapper empMapper;
    @Override
    public List<Emp> findEmp() {
        List<Emp> emps = empMapper.selectEmpAll();
        return emps;
    }

    @Override
    public List<EmpBusiness> findEmpBusiness(List<Emp> emps,String year,String month) {
        List<EmpBusiness> empBusinesses = new ArrayList<EmpBusiness>();
        int i=1;
        for (Emp emp : emps) {
            EmpBusiness empBusiness=new EmpBusiness();

            String empName = emp.getEmployeeName();
            //查询改员工业务:单据分发完成情况(数量）
            int releaseBillNumber = empBusinessMapper.selectReleaseBillNumber(empName,year,month);
            //查询改员工业务://货运单主表填票人
            int writeGoodsBillMasterNumber = empBusinessMapper.writeGoodsBillMasterNumber(empName,year,month);
            //查询改员工业务://承运人
            int driverWayNumber = empBusinessMapper.driverWayNumber(empName,year,month);
            //查询改员工业务://司机
//            int driverNumber = empBusinessMapper.driverNumber(empName,year,month);
            //查询改员工业务:投诉处理
            int complaintDealNumber = empBusinessMapper.complaintDealNumber(empName,year,month);
            //查询改员工业务://回告信息填写
            int callBackWriterNumber = empBusinessMapper.callBackWriterNumber(empName,year,month);

            empBusiness.setReleaseBillNumber(releaseBillNumber);
            empBusiness.setWriteGoodsBillMasterNumber(writeGoodsBillMasterNumber);
            empBusiness.setDriverWayNumber(driverWayNumber);
//            empBusiness.setDriverNumber(driverNumber);
            empBusiness.setComplaintDealNumber(complaintDealNumber);
            empBusiness.setCallBackWriterNumber(callBackWriterNumber);

            empBusiness.setPosition(emp.getPosition());
            empBusiness.setDepartment(emp.getDepartment());
            empBusiness.setName(emp.getEmployeeName());
            empBusiness.setEmpCode(emp.getEmployeeCode());

            empBusinesses.add(empBusiness);
            System.out.println("第"+i+++"个，时间为："+new Date());
        }
        return empBusinesses;
    }

    @Override
    public GridResult<EmpBusiness> queryByPage(GridRequest gridRequest,int size, String year, String month) {
        // 获取当前页和页大小
        Integer currentPage = gridRequest.getPage();
        Integer pageSize = gridRequest.getLimit();

        // 判断是否有排序,有则生成符合pagehelper的排序字符串
        String sidx = gridRequest.getSidx(); // 排序参数
        String sord = gridRequest.getSord(); // 排序规则

        System.out.println(currentPage);
        System.out.println(pageSize);
        System.out.println(sidx);
        System.out.println(sord);

        String orderBy = "";
        // pagehelper静态设置页属性方法，它会拦截下面的第一个查询！第二个无效。
        if(checkOrderBy(sidx, sord)){
            sidx = upperCharToUnderLine(sidx);
            orderBy = sidx+" "+sord;

            System.out.println(orderBy);

            PageHelper.startPage(currentPage, pageSize, orderBy);
        }else{
            // 设置当前页和页大小
            PageHelper.startPage(currentPage, pageSize);
        }

        List<Emp> emps=empMapper.selectEmpAll();
        System.out.println("emps:"+emps.size());
        // 查询结果（传入排序）
        List<EmpBusiness> result = new LinkedList<EmpBusiness>();
        System.out.println("时间1："+new Date());
        for (Emp emp : emps) {
            EmpBusiness empBusiness=new EmpBusiness();

            String empName = emp.getEmployeeName();
            //查询改员工业务:单据分发完成情况(数量）
            int releaseBillNumber = empBusinessMapper.selectReleaseBillNumber(empName,year,month);
            //查询改员工业务://货运单主表填票人
            int writeGoodsBillMasterNumber = empBusinessMapper.writeGoodsBillMasterNumber(empName,year,month);
            //查询改员工业务://承运人
            int driverWayNumber = empBusinessMapper.driverWayNumber(empName,year,month);
            //查询改员工业务://司机
//            int driverNumber = empBusinessMapper.driverNumber(empName,year,month);
            //查询改员工业务:投诉处理
            int complaintDealNumber = empBusinessMapper.complaintDealNumber(empName,year,month);
            //查询改员工业务://回告信息填写
            int callBackWriterNumber = empBusinessMapper.callBackWriterNumber(empName,year,month);

            empBusiness.setReleaseBillNumber(releaseBillNumber);
            empBusiness.setWriteGoodsBillMasterNumber(writeGoodsBillMasterNumber);
            empBusiness.setDriverWayNumber(driverWayNumber);
//            empBusiness.setDriverNumber(driverNumber);
            empBusiness.setComplaintDealNumber(complaintDealNumber);
            empBusiness.setCallBackWriterNumber(callBackWriterNumber);

            empBusiness.setPosition(emp.getPosition());
            empBusiness.setDepartment(emp.getDepartment());
            empBusiness.setName(emp.getEmployeeName());
            empBusiness.setEmpCode(emp.getEmployeeCode());

            result.add(empBusiness);

        }
        System.out.println("时间2："+new Date());

        // 利用当前页和页大小配置pageInfo
        PageInfo<EmpBusiness> pageList = new PageInfo<>(result);
        System.out.println("pageList::"+pageList);
        // 新建一个封装类对象，设置参数，传入查询得到的数据
        GridResult<EmpBusiness> grid = new GridResult<>();

        // 存入查询数据
        grid.setData(result);
        // 存入总条数
        grid.setCount(size);

        return grid;
    }


    // 判断是否非空
    private boolean checkOrderBy(String sidx,String sord){
        if(sidx != null && sord != null
                && sidx.trim().length() > 0
                && sord.trim().length() > 0){
            return true;
        }
        return false;
    }
    // 大写转换为下划线+小写
    public static String upperCharToUnderLine(String param) {
        Pattern p = Pattern.compile("[A-Z]");
        if (param == null || param.equals("")) {
            return "";
        }
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param);
        int i = 0;
        while (mc.find()) {
            System.out.println(builder.toString());
            System.out.println("mc.start():" + mc.start() + ", i: " + i);
            System.out.println("mc.end():" + mc.start() + ", i: " + i);
            builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
            i++;
        }

        if ('_' == builder.charAt(0)) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }
}
